// pages/goods/goods.js
const interfaces = require("../../utils/request.js");
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    gallery: [],
    info: {},
    attribute: [],
    issue: [],
    productList: [],
    isMask: true,
    numCount: 1,   //商品数量
    badgeCount: 0,  //不同商品的个数
    goodsId: '',
    isCollection: false //收藏
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options)
    this.getGoodsDetail(options.id);
    this.getCard();
  },
  showType(){
    this.setData({
      isMask: false
    })
  },
  toCart(){
    wx.switchTab({
      url: '/pages/cart/cart',
    })
  },
  toIndex(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  exit(){
    this.setData({
      isMask: true
    })
  },
  getCount(e){
    //console.log(e)
    this.setData({
      numCount: e.detail
    })
  },
  /**
   * 加入购物车
   */
  addCard(){
    let login = wx.getStorageSync('isLogin');
    let openId = wx.getStorageSync('openId');
    let self = this;
    wx.showLoading({
      title: ''
    });
    if (login){
      wx.request({
        url: interfaces.addCard,
        method: "POST",
        data:{
          openId: openId,
          goodsId: this.data.goodsId,
          number: this.data.numCount
        },
        success(res){
          wx.hideLoading();
          wx.showToast({
            title: "加入购物车成功",
            icon: "none"
          });
          self.getCard();
          //console.log(res)
        }
      })
    }else{
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
  },
  /**
   * 获取不同商品的总数量
   */
  getCard(){
    let openId = wx.getStorageSync('openId');
    let self = this;
    if(openId){
      wx.request({
        url: interfaces.cartList,
        data:{
          openId: openId
        },
        success(res){
          console.log(res)
          let cardList = res.data.data;
          self.setData({
            badgeCount: cardList.length
          })
        }
      })
    }else{
      return 
    }
  },
  /**
   * 获取商品详情数据
   */
  getGoodsDetail(id){
    let openId = wx.getStorageSync('openId');
    const self = this;
    wx.showLoading({
      title: ''
    });
    wx.request({
      url: interfaces.goodsDetail,
      data:{
        id: id,
        openId: openId
      },
      success(res){
        console.log(res)
        wx.hideLoading();
        self.setData({
          gallery: res.data.gallery,
          info: res.data.info,
          attribute: res.data.attribute,
          issue: res.data.issue,
          productList: res.data.productList,
          goodsId: res.data.info.id,
          isCollection: res.data.collected
        })
        WxParse.wxParse('goodsDetail', 'html', res.data.info.goods_desc, self);
      }
    })
  },
  toGoods(e){
    let id = e.currentTarget.dataset.id;
    wx.redirectTo({
      url: '/pages/goods/goods?id=' + id,
    })
  },
  /**
   * 收藏
   */
  collect(){
    let openId = wx.getStorageSync('openId')
    const self = this
    this.setData({
      isCollection: !this.data.isCollection
    })
    console.log(this.data.isCollection)
    wx.request({
      url: interfaces.saveCollect,
      method: "POST",
      data:{
        openId: openId,
        goodsId: self.data.goodsId
      },
      success(res){
        console.log(res)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})